<?php
namespace Portal\Controller;
use Common\Controller\HomebaseController;
class HPayController extends HomebaseController {
	function _initialize() {
		parent::_initialize();
		vendor("WechatSDK.WxPay.JsApiPay");
	}
	
	public function wxpay(){
		Vendor("WechatSDK.WxPay.JsApiPay");
		$config = new \WxPayConfig();
		$fee = session("totoal_fee");
		$Api = new \WxPayApi();
		$_url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		//①、获取用户openid
		$tools = new \JsApiPay();
		if($otherid)$openId = $otherid;
		else $openId = $tools->GetOpenid($_url);
		//②、统一下单
//		$openId = 'ozTzev-L7868coSN2YZCe6P5AcuY';
		$input = new \WxPayUnifiedOrder();
		$input->SetBody("充值");
		$input->SetAttach("充值");
		$input->SetOut_trade_no($config::MCHID.date("YmdHis"));
		$input->SetTotal_fee($fee);
		$input->SetTime_start(date("YmdHis"));
		$input->SetTime_expire(date("YmdHis", time() + 600));
		$input->SetGoods_tag(" ");
		$input->SetNotify_url("http://mua.ocmcom.com/LotteryMobile/paynotify");
		$input->SetTrade_type("JSAPI");
		$input->SetOpenid($openId);
		$order = $Api::unifiedOrder($input);
		$jsApiParameters = $tools->GetJsApiParameters($order);
		//获取共享收货地址js函数参数
		$editAddress = $tools->GetEditAddressParameters();
		$this->jsApiParameters = $jsApiParameters;
		$this->editAddress = $editAddress;
		$this->appid = $config::APPID;
		$this->fee=$fee;
		$this->display();
	}

	public function wxpayOK(){
		$res = M()->execute($sql);
		redirect("/HUser/subscribe");
		
	}

	public function alipay(){
		$needmny = session("needpay");
		$this->good = "账户充值";
		$this->fee = $needmny;
		$this->display();
	}
	
	public function alisubmit(){
		header("Content-type:text/html;charset=utf-8");
		import('Vendor.AliPaySDK.lib.alipay_submit');
		require_once("./simplewind/Core/Library/Vendor/AliPaySDK/alipayconfig.php");
        //商户订单号，商户网站订单系统中唯一订单号，必填
//      $out_trade_no = session("order_no");
        $out_trade_no = $_POST['WIDout_trade_no'];
        //订单名称，必填
        $subject = $_POST['WIDsubject'];
        //付款金额，必填
        $total_fee = $_POST['WIDtotal_fee'];
        //商品描述，可空
        $body = $_POST['WIDbody'];
		//构造要请求的参数数组，无需改动
		$user = sp_get_current_user();
		$Cad = M("CapitalDynamic");
		$data['user_uniq_id'] = $user['user_uniq_id'];
		$data['des'] = '充值'.$total_fee."元";
		$data['order_no'] = $out_trade_no;
		$data['money'] = $total_fee*1;
		$rrs = $Cad->add($data);
		$alipaySubmit = new \AlipaySubmit($alipay_config);
		$parameter = array(
				"service"       => $alipay_config['service'],
				"partner"       => $alipay_config['partner'],
				"seller_id"     => $alipay_config['seller_id'],
				"payment_type"	=> $alipay_config['payment_type'],
				"notify_url"	=> $alipay_config['notify_url'],
				"return_url"	=> $alipay_config['return_url'],
				
				"anti_phishing_key"=>$alipaySubmit->query_timestamp(),
				"exter_invoke_ip"=>$alipay_config['exter_invoke_ip'],
				"out_trade_no"	=> $out_trade_no,
				"subject"	=> $subject,
				"total_fee"	=> $total_fee,
				"body"	=> $body,
				"_input_charset"	=> trim(strtolower($alipay_config['input_charset']))
				//其他业务参数根据在线开发文档，添加参数.文档地址:https://doc.open.alipay.com/doc2/detail.htm?spm=a219a.7629140.0.0.kiX33I&treeId=62&articleId=103740&docType=1
		        //如"参数名"=>"参数值"
		);
//		var_dump($parameter);
//		return;
		//建立请求
		$html_text = $alipaySubmit->buildRequestForm($parameter,"post", "确认");
		echo $html_text;
	}
		
	public function alinotify_url(){
		require_once("./simplewind/Core/Library/Vendor/AliPaySDK/alipayconfig.php");
		Vendor("AliPaySDK.alipayconfig");
		import('Vendor.AliPaySDK.lib.alipay_notify');
		$alipayNotify = new \AlipayNotify($alipay_config);
		$verify_result = $alipayNotify->verifyReturn();
		if($verify_result) {//验证成功
			//请在这里加上商户的业务逻辑程序代
			//商户订单号
			$out_trade_no = i('out_trade_no');
			//支付宝交易号
			$trade_no = i('trade_no');
			//交易状态
			$trade_status = i('trade_status');
		    if(i('trade_status') == 'TRADE_FINISHED') {
		    }
		    else if (i('trade_status') == 'TRADE_SUCCESS') {
				$data = session("payeddata");
				if(isset($data)){
					$Ad = M("Ad");
					$Ad->add($data);
					$adid = M()->getLastInsID();
					$Stand = M("Stand");
					$stand_id = $data['stand_id'];
					$Stand->where("id = $stand_id")->setField("ad_id",$adid);
					$imgs = session("gridimgs");
					$Imglis = M("ImgLib");
					for($i =0;$i<count($imgs);++$i){
						$il['ad_id'] = $adid;
						$il['is_first'] = $i==0?2:1;
						$il['icon'] = $imgs[$i];
						$Imglis->add($il);
					}
					$resmess = M("Constant")->where("id = 25")->select();
					$mess = explode(",",$resmess[0]['pvalue']);
					for($i =0,$len=count($mess);$i<$len;$i++){
						xw_send_code($mess[$i],'您有新的广告审核，请登录查看'.$_SERVER['HTTP_HOST'].'/Admin/');
					}
				}
				$user = sp_get_current_user();
				$Cad = M("CapitalDynamic")->where("user_uniq_id = '".$user['user_uniq_id']."'")->select();
				$user['balance'] = $user['balance']*1+1*$Cad[0]['money'];
				$rr = M("AppUser")->where("user_uniq_id ='".$user['user_uniq_id']."'")->data($user)->save();
//				$rr =M("AppUser")->where("user_uniq_id ='".$user['user_uniq_id']."'")->setField("balance",$user['balance']*1+100*$Cad[0]['money']);
				if($rr)session("user",$user);
		    }
			echo "success";		//请不要修改或删除
		}
		else {
		    echo "fail";
		}
//		var_dump($verify_result);
//		var_dump($user);
//		var_dump(session("needpay"));
//		return;
		redirect("/HUser/user");
	}

	function setmny(){
		session("needpay",$_POST['mny']);
		$this->success(1);
	}
}